package com.msg.test.model;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

/**
 * Created by Administrator on 2017/10/20 0020.
 */
public class MsgDeliver extends MsgHead{
    private static Log log = LogFactory.getLog(MsgDeliver.class);
    private long msg_Id;
    private String dest_Id;
    private String service_Id;
    private byte tP_pid = 0;
    private byte tP_udhi = 0;
    private byte msg_Fmt = 15;
    private String src_terminal_Id; //源终端MSISDN
    private byte src_terminal_type = 0; //源终端号码类型
    private byte registered_Delivery = 0;//是否为状态报告
    private byte msg_Length;
    private String msg_Content;
    private String linkID;

    private long msg_Id_report;
    private String stat;
    private String submit_time;
    private String done_time;
    private String dest_terminal_Id;
    private int sMSC_seqence;
    private int result;//解析结果
    public MsgDeliver(byte[] data){
        if(data.length > 8+8+21+10+1+1+1+32+1+1+1+20){
            String fmtStr = "gb2312";
            ByteArrayInputStream bins = new ByteArrayInputStream(data);
            DataInputStream dins = new DataInputStream(bins);
            try{
                this.setTotalLength(data.length + 4);
                this.setCommandId(dins.readInt());
                this.setSequence(dins.readInt());
                this.msg_Id = dins.readLong();
                byte[] destIdByte = new byte[21];
                dins.read(destIdByte);
                this.dest_Id = new String(destIdByte);
                byte[] service_IdByte = new byte[10];
                dins.read(service_IdByte);
                this.service_Id = new String(service_IdByte);
                this.tP_pid = dins.readByte();
                this.tP_udhi = dins.readByte();
                this.msg_Fmt = dins.readByte();
                fmtStr = this.msg_Fmt==8?"utf-8":"gb2312";
                byte[] src_terminal_IdByte = new byte[32];
                dins.read(src_terminal_IdByte);
                this.src_terminal_Id = new String(src_terminal_IdByte);
                this.src_terminal_type = dins.readByte();
                this.registered_Delivery = dins.readByte();
                this.msg_Length = dins.readByte();//消息长度
                byte[] msg_ContentByte = new byte[msg_Length];
                dins.read(msg_ContentByte);
                if (registered_Delivery == 1){
                    this.msg_Content = new String(msg_ContentByte,fmtStr);
                    if (msg_Length == 8+7+10+10+21+4){
                        ByteArrayInputStream binsC = new ByteArrayInputStream(data);
                        DataInputStream dinsC = new DataInputStream(binsC);
                        this.msg_Id_report = dinsC.readLong();
                        byte[] startByte = new byte[7];
                        dinsC.read(startByte);
                        this.stat = new String(startByte,fmtStr);
                        byte[] submit_timeByte = new byte[10];
                        dins.read(submit_timeByte);
                        this.submit_time = new String(submit_timeByte,fmtStr);
                        byte[] done_timeByte = new byte[7];
                        dinsC.read(done_timeByte);
                        this.done_time = new String(done_timeByte,fmtStr);
                        byte[] dest_terminal_IdByte = new byte[21];
                        dinsC.read(dest_terminal_IdByte);
                        this.dest_terminal_Id = new String(dest_terminal_IdByte,fmtStr);
                        this.sMSC_seqence = dinsC.readInt();
                        dinsC.close();
                        binsC.close();
                        this.result = 0;
                    }else {
                        this.result = 1;
                    }
                }else {
                    this.msg_Content = new String(msg_ContentByte,fmtStr);//消息长度
                }
                byte[] linkIDByte = new byte[20];
                this.linkID = new String(linkIDByte);
                this.result = 0;
                dins.close();
                bins.close();
            }catch (IOException ex){
                this.result = 8;
            }
        }
    }

    public long getMsg_Id() {
        return msg_Id;
    }

    public void setMsg_Id(long msg_Id) {
        this.msg_Id = msg_Id;
    }

    public String getDest_Id() {
        return dest_Id;
    }

    public void setDest_Id(String dest_Id) {
        this.dest_Id = dest_Id;
    }

    public String getService_Id() {
        return service_Id;
    }

    public void setService_Id(String service_Id) {
        this.service_Id = service_Id;
    }

    public byte gettP_pid() {
        return tP_pid;
    }

    public void settP_pid(byte tP_pid) {
        this.tP_pid = tP_pid;
    }

    public byte gettP_udhi() {
        return tP_udhi;
    }

    public void settP_udhi(byte tP_udhi) {
        this.tP_udhi = tP_udhi;
    }

    public byte getMsg_Fmt() {
        return msg_Fmt;
    }

    public void setMsg_Fmt(byte msg_Fmt) {
        this.msg_Fmt = msg_Fmt;
    }

    public String getSrc_terminal_Id() {
        return src_terminal_Id;
    }

    public void setSrc_terminal_Id(String src_terminal_Id) {
        this.src_terminal_Id = src_terminal_Id;
    }

    public byte getSrc_terminal_type() {
        return src_terminal_type;
    }

    public void setSrc_terminal_type(byte src_terminal_type) {
        this.src_terminal_type = src_terminal_type;
    }

    public byte getRegistered_Delivery() {
        return registered_Delivery;
    }

    public void setRegistered_Delivery(byte registered_Delivery) {
        this.registered_Delivery = registered_Delivery;
    }

    public byte getMsg_Length() {
        return msg_Length;
    }

    public void setMsg_Length(byte msg_Length) {
        this.msg_Length = msg_Length;
    }

    public String getMsg_Content() {
        return msg_Content;
    }

    public void setMsg_Content(String msg_Content) {
        this.msg_Content = msg_Content;
    }

    public String getLinkID() {
        return linkID;
    }

    public void setLinkID(String linkID) {
        this.linkID = linkID;
    }

    public long getMsg_Id_report() {
        return msg_Id_report;
    }

    public void setMsg_Id_report(long msg_Id_report) {
        this.msg_Id_report = msg_Id_report;
    }

    public String getStat() {
        return stat;
    }

    public void setStat(String stat) {
        this.stat = stat;
    }

    public String getSubmit_time() {
        return submit_time;
    }

    public void setSubmit_time(String submit_time) {
        this.submit_time = submit_time;
    }

    public String getDone_time() {
        return done_time;
    }

    public void setDone_time(String done_time) {
        this.done_time = done_time;
    }

    public String getDest_terminal_Id() {
        return dest_terminal_Id;
    }

    public void setDest_terminal_Id(String dest_terminal_Id) {
        this.dest_terminal_Id = dest_terminal_Id;
    }

    public int getsMSC_seqence() {
        return sMSC_seqence;
    }

    public void setsMSC_seqence(int sMSC_seqence) {
        this.sMSC_seqence = sMSC_seqence;
    }

    public int getResult() {
        return result;
    }

    public void setResult(int result) {
        this.result = result;
    }
}
